/**
 * 
 */
package com.ch01;
import java.util.*;

/**
 * @author Swetha
 *
 */
public class UserBean {

	public static final boolean INVALID = false;
	public static final boolean VALID = true;
	
	private String userName;
	private String userRights;
	private ArrayList<String> userList_Validate;
	
	public UserBean()
	{
		userList_Validate = new ArrayList<String>();
	}
	
	public String getUserName()
	{
		return userName;
	}
	
	public void setUserName(String uname)
	{
		userName = uname;
	}
	
	public String getUserRights()
	{
		return userRights;
	}
	
	public void setUserRights(String urights)
	{
		userRights = urights;
	}
	
	// print the list of validation errors
	public void printValidationList()
	{
		for (String s : userList_Validate)
		{
			System.out.println(s);
		}
	}
	
	// clears the validation list
	public void clearValidationList()
	{
		userList_Validate.clear();
	}
	
	public boolean Validate(String uname, String pwd) 
	{
		this.userName = uname;
		
		int i = 0;
		boolean isValid = true;
		
		clearValidationList();
		
		if (uname != null || pwd != null)
		{
			if (uname.toLowerCase() == "admin")
			{
			    setUserRights("administrator");
			    return VALID;
		    }
		    else 
		    {
			    setUserRights("user");
		    }
 			
			if (!userNameLength(uname))
			{
				userList_Validate.add("User Name length should be > 8 and < 20");
				isValid = false;
			}
			if (!userNameStartLetter(uname))
			{
				userList_Validate.add("User Name should start with a letter");
				isValid = false;
			}
			if (!userNameContains(uname))
			{
				userList_Validate.add("User Name should not contain space, % , + - &");
				isValid = false;
			}
			if (!passwordLength(pwd))
			{
				userList_Validate.add("Password length should be exactly 8 characters");
				isValid = false;
			}
			if (!passwordContains(pwd))
			{
				userList_Validate.add("Password should contain atleast one character and one number");
				isValid = false;
			}

            if (isValid)
			{
				return VALID;
			}
            else
            {
            	return INVALID;
            }
            
			/* if (userNameLength(uname) && userNameStartLetter(uname) && userNameContains(uname)
			    && passwordLength(pwd) && passwordContains(pwd))
		    {
			    return VALID;
		    } 
		    else
		    {
		    	return INVALID;
		    } */
		}
		else {
			System.out.println("User Name or  password is null");
			return INVALID;
		}
		
	}
	
	private boolean userNameLength(String uname)
	{
		if ((uname.length()) > 8 && (uname.length() < 20 ))
		{
			return VALID;
		}
		else
		{
			return INVALID;
		}
	}
	
	private boolean userNameStartLetter(String uname)
	{
		if (Character.isDigit(uname.charAt(0)))
		{
		 	return INVALID;
		}
		else
		{
			return VALID;
		}
	}
	
	private boolean userNameContains(String uname)
	{
		String[] st_arr = {" ", "&", "%", ",","+","-"};
	 
		for (String i : st_arr)
		{
			if (uname.contains(i)) 
			{
				return INVALID;
			}
		}
		return VALID;
	}
	
	private boolean passwordLength(String pwd)
	{
		if (pwd.length() != 8)
		{
			return INVALID;
		}
		else
		{
			return VALID;
		}
	}
	
	private boolean passwordContains(String pwd)
	{
		char[] ch_arr;
		boolean letter_flag = INVALID;
		boolean digit_flag = INVALID;
		
		ch_arr = pwd.toCharArray();
		
		for (char ch : ch_arr) 
		{
			if (Character.isLetter(ch))
			{
				letter_flag = VALID;
			}
			else if (Character.isDigit(ch))
			{
			    digit_flag = VALID;
			}
			
			if (letter_flag && digit_flag)
			{
				return VALID;
			}
		}
		
		return INVALID;
	}

		
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
    UserBean userbean = new UserBean();
    
    
	boolean validate_flag;
		
	userbean.setUserName("Swetha123");
	validate_flag = userbean.Validate("Swetha123", "A1234567");
	System.out.println("User " + userbean.getUserName() + " Validates to " + validate_flag +
			           " user rights is " + userbean.getUserRights()); 
	userbean.printValidationList();
	System.out.println("-----------------------------------------------------------------");
	
	userbean.setUserName("123Swetha");
	validate_flag = userbean.Validate("123Swetha","__123456");
	System.out.println("User " + userbean.getUserName() + " Validates to " + validate_flag +
	                   " user rights is " + userbean.getUserRights());
	userbean.printValidationList();
	System.out.println("-----------------------------------------------------------------");
	
	userbean.setUserName("admin");
	validate_flag = userbean.Validate("admin","1234ABCD");
	System.out.println("User " + userbean.getUserName() + " Validates to " + validate_flag +
	                   " user rights is " + userbean.getUserRights()); 
	userbean.printValidationList();
	System.out.println("-----------------------------------------------------------------");
	
	userbean.setUserName("Swetha");
	validate_flag = userbean.Validate("Swetha","Swetha");
	System.out.println("User " + userbean.getUserName() + " Validates to " + validate_flag +
	                   " user rights is " + userbean.getUserRights());
	userbean.printValidationList();
	System.out.println("-----------------------------------------------------------------");
	
	userbean.setUserName("Swetha%");
	validate_flag = userbean.Validate("Swetha%","Swetha12");
	System.out.println("User " + userbean.getUserName() + " Validates to " + validate_flag +
	                   " user rights is " + userbean.getUserRights());
	userbean.printValidationList();
	System.out.println("-----------------------------------------------------------------");
			
	}

}
// arraylist for all the invalid username pwd, different i/p options